﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmMenu : Office2007Form
    {
        #region Fields
        MenuCtrl m_MenuCtrl = new MenuCtrl();
        UnitCtrl m_UnitCtrl = new UnitCtrl();
        CategoryCtrl m_CategoryCtrl = new CategoryCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmMenu()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmMenu_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_UnitCtrl.LoadToComboBox(cmbUnit);
            m_CategoryCtrl.LoadToComboBox(cmbCate);
            m_CategoryCtrl.LoadToComboBox(cmbCateSearch);
            m_CategoryCtrl.LoadToComboBox(a_cmbCategory);
            m_CategoryCtrl.LoadToDataGridViewComboBoxColumn(colCateId);
            m_UnitCtrl.LoadToComboBox(a_cmbUnit);
            m_UnitCtrl.LoadToDataGridViewComboBoxColumn(colUnitId);
            m_MenuCtrl.LoadToDataGridViewComboBoxColumnState(colState);
            m_MenuCtrl.Show(dgvMenu, bindingNavigatorMenu, cmbCate, txtMenuId, txtMenuName, txtPrice, cmbUnit, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            try
            {
                MenuInfo info = new MenuInfo(txtMenuId.Text, cmbCate.SelectedValue.ToString(), cmbUnit.SelectedValue.ToString(), txtMenuName.Text, Convert.ToDouble(txtPrice.Text), cmbState.SelectedIndex , txtRemark.Text);
                if (isNew == true)
                {
                    m_MenuCtrl.Add(info);
                } 
                else
                {
                    m_MenuCtrl.Update(info);
                }
                m_MenuCtrl.Show(dgvMenu, bindingNavigatorMenu, cmbCate, txtMenuId, txtMenuName, txtPrice, cmbUnit, cmbState, txtRemark);
                SaveItem.Enabled = false;
                isNew = false;
            }
            catch
            {
                MessageBoxEx.Show("Giá không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            m_MenuCtrl.Show(dgvMenu, bindingNavigatorMenu, cmbCate, txtMenuId, txtMenuName, txtPrice, cmbUnit, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }

        private void btnAddCate_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormCategory();
        }

        private void btnAddUnit_Click(object sender, EventArgs e)
        {
            ShowForm.ShowFormUnit();
        }
        #endregion

        #region BindingNavigator Event Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvMenu.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_MenuCtrl.AddNewRow();
            m_Row["MenuId"] = GetID.Menu(dgvMenu);
            m_Row["CateId"] = "";
            m_Row["UnitId"] = "";
            m_Row["MenuName"] = "";
            m_Row["Price"] = "0";
            m_Row["State"] = 1;
            m_Row["Remark"] = "";
            m_MenuCtrl.AddNewMenu(m_Row);
            bindingNavigatorMenu.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_MenuCtrl.Show(dgvMenu, bindingNavigatorMenu, cmbCate, txtMenuId, txtMenuName, txtPrice, cmbUnit, cmbState, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvMenu.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvMenu.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvMenu.SelectedRows)
                    {
                        bindingNavigatorMenu.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvMenu.Rows)
            {
                if (row.Cells["colMenuId"].Value.ToString() == "" || row.Cells["colMenuName"].Value.ToString() == "")
                {
                    check = false;
                }
                try
                {
                    double priceCheck = Convert.ToDouble(row.Cells["colPrice"].Value.ToString());
                }
                catch
                {
                	check = false;
                }
            }
            if (check)
            {
                if (!m_MenuCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện ! Vui lòng thao tác lại.", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Searching
        private void rbMenuId_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMenuId.Checked == true)
            {
                lblSearch.Text = "Mã món ăn :";
                lblMin.Visible = false;
                lblMax.Visible = false;
                txtMin.Visible = false;
                txtMax.Visible = false;
                cmbCateSearch.Visible = false;
                txtKeyWord.Visible = true;
            }
        }

        private void rbMenuName_CheckedChanged(object sender, EventArgs e)
        {
            if (rbMenuName.Checked == true)
            {
                lblSearch.Text = "Tên món ăn :";
                lblMin.Visible = false;
                lblMax.Visible = false;
                txtMin.Visible = false;
                txtMax.Visible = false;
                cmbCateSearch.Visible = false;
                txtKeyWord.Visible = true;
            }
        }

        private void rbCateId_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCateId.Checked == true)
            {
                lblSearch.Text = "Danh mục :";
                lblMin.Visible = false;
                lblMax.Visible = false;
                txtMin.Visible = false;
                txtMax.Visible = false;
                cmbCateSearch.Visible = true;
                txtKeyWord.Visible = false;
            }
        }

        private void rbPrice_CheckedChanged(object sender, EventArgs e)
        {
            if (rbPrice.Checked == true)
            {
                lblSearch.Text = "Khoảng giá :";
                lblMin.Visible = true;
                lblMax.Visible = true;
                txtMin.Visible = true;
                txtMax.Visible = true;
                cmbCateSearch.Visible = false;
                txtKeyWord.Visible = false;
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtSource = new DataTable();
                if (rbMenuId.Checked == true)
                {
                    dtSource = m_MenuCtrl.GetByLikeId(txtKeyWord.Text);
                }
                else if (rbMenuName.Checked == true)
                {
                    dtSource = m_MenuCtrl.GetByName(txtKeyWord.Text);
                }
                else if (rbCateId.Checked == true)
                {
                    dtSource = m_MenuCtrl.GetByCate(cmbCateSearch.SelectedValue.ToString());
                }
                else if (rbPrice.Checked == true)
                {
                    try
                    {
                        dtSource = m_MenuCtrl.GetByPrice(Convert.ToDouble(txtMin.Text), Convert.ToDouble(txtMax.Text));
                    }
                    catch
                    {
                        MessageBoxEx.Show("Khoảng giá không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    dtSource = m_MenuCtrl.GetByName(txtKeyWord.Text);
                }

                m_MenuCtrl.ShowWithSource(dgvMenu, bindingNavigatorMenu, cmbCate, txtMenuId, txtMenuName, txtPrice, cmbUnit, cmbState, txtRemark, dtSource);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void btnTKA_Click(object sender, EventArgs e)
        {
            try
            {
                String menuId = "";
                String menuName = "";
                String cateId = "-1";
                String unitId = "-1";
                bool price = false;
                double minPrice = 0.0;
                double maxPrice = 0.0;

                if (chkMenuId.Checked == true)
                {
                    menuId = a_txtMenuID.Text;
                }
                if (chkMenuName.Checked == true)
                {
                    menuName = a_txtMenuName.Text;
                }
                if (chkCateId.Checked == true)
                {
                    cateId = a_cmbCategory.SelectedValue.ToString();
                }
                if (chkUnitId.Checked == true)
                {
                    unitId = a_cmbUnit.SelectedValue.ToString();
                }
                if (chkPrice.Checked == true)
                {
                    price = true;
                    minPrice = Convert.ToDouble(a_txtMin.Text);
                    maxPrice = Convert.ToDouble(a_txtMax.Text);
                }

                DataTable dtSource = m_MenuCtrl.GetByOption(menuId, menuName, cateId, unitId, price, minPrice, maxPrice);
                m_MenuCtrl.ShowWithSource(dgvMenu, bindingNavigatorMenu, cmbCate, txtMenuId, txtMenuName, txtPrice, cmbUnit, cmbState, txtRemark, dtSource);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Khoảng giá cần nhập đầy đủ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region DataGridView Event
        private void dgvMenu_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }

        private void dgvMenu_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBoxEx.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        #endregion

        #region Context Menu Event Click
        private void ExitItemContext_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }

        private void AddNewItemContext_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContext_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContext_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContext_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }
        #endregion
    }
}
